var OnLoad = Class.create();

Object.extend(OnLoad, {
  onLoad: function() { 
    this.applyDormantInputs();
    this.applyActivatable();
    this.enableForms();
    this.fillInForms();
    Quotes.applyTranslatable();
    Collapsible.applyCollapsible();
  },
  
  applyDormantInputs: function() {
    $$('input.dormant').each(function(el) {
      el.observe("focus", OnLoad.wakeAction);
    });
    $$('textarea.dormant').each(function(el) {
      el.observe("focus", OnLoad.wakeAction);
    });
  },

  wakeAction: function(ev) {
    this.setAttribute('dormant_value', this.value);
    this.value = '';
    this.removeClassName('dormant');
    this.stopObserving('focus', OnLoad.wakeAction);
    this.observe('blur', OnLoad.dormantAction);
  },
  
  dormantAction: function(ev) {
    if (this.value == '') {
      this.value = this.getAttribute('dormant_value');
      this.addClassName('dormant');
      this.observe('focus', OnLoad.wakeAction);
    }
    else {
      this.stopObserving('blur', OnLoad.dormantAction);
    }
  },

  applyActivatable: function() {
    $$('.activatable').each(function(el) {
      Event.observe(el, "mouseover", function() { 
        el.removeClassName('inactive'); 
        el.addClassName('active'); 
      });
      Event.observe(el, "mouseout", function(ev) { 
        // Don't process this event if called from child elements.
        // http://www.quirksmode.org/js/events_mouse.html#mouseover
        if (!ev) var ev = window.event;
        var tg = (window.event) ? ev.srcElement : ev.target;
        var reltg = (ev.relatedTarget) ? ev.relatedTarget : ev.toElement;
        
        // If the element moved to is a child of the activatable, return.
        try {
          while (reltg && reltg != tg && reltg != el && reltg.nodeName != 'BODY')
            reltg = reltg.parentNode;
        }
        catch(err) {
          return;
        }
        if ((reltg == tg) || (reltg == el)) return;

        el.removeClassName('active'); 
        el.addClassName('inactive'); 
      });
      
      el.removeClassName('activatable');
      el.addClassName('inactive');
    });
  },
  
  enableForms: function() {
    $$('form').each(function(f) {
      if (f.hasClassName('default_enable')) {
        f.enable();
      }
    });
  },
  
  fillInForms: function() {
    name = Application.getName();
    if ((name) && (name != "null")) {
      $$('form').each(function(f) {
        f.getInputs('text', 'name').each(function(el) {
          if (el.hasClassName('comment_name')) {
            el.value = name;
            el.removeClassName('dormant');
            el.stopObserving('focus', OnLoad.wakeAction);
          }
        });
      });
    }

    email = Application.getEmail();
    if ((email) && (email != "null")) {
      $$('form').each(function(f) {
        f.getInputs('text', 'email').each(function(el) {
          if (el.hasClassName('comment_email')) {
            el.value = email;
            el.removeClassName('dormant');
            el.stopObserving('focus', OnLoad.wakeAction);
          }
        });
      });
    }
  }
});

Event.observe(window, "load", function() {
  OnLoad.onLoad();
});

Ajax.Responders.register({
  onComplete: function() {
    OnLoad.onLoad();
  }
});
